package com.iatinspur.controller;

import com.iatinspur.dubbo.service.ProviderService;
import com.iatinspur.dubbo.service.ProviderStreamService;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;

@RestController
public class ConsumerController {
    private String name="绿飞";

    @DubboReference(version = "1.0.0")
    private ProviderService providerService;

    @DubboReference(version = "1.0.0")
    private ProviderStreamService streamService;



    @GetMapping("/getResult")
    public String getResult(){
        return this.name+"，"+providerService.getInfo();
    }

    @GetMapping("/dubboServerStream")
    public String dubboServerStream(){
        StringBuffer sb=new StringBuffer();
        /**
         * CountDownLatch 设置初始值1
         */
        final CountDownLatch latch = new CountDownLatch(1);
        // 这里其实已经开启线程了
        // todo 这里可不可能存在负载均衡？我猜 不可能
        streamService.dubboServerStream(this.name, new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.err.println("收到："+data);
                sb.append(data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.err.println("收到,完毕");
                sb.append("-----》收到,完毕");
                // 执行完了 就减1
                latch.countDown();
            }
        });


        try {
            // 这里等待 当latch 为0 就能放行
            latch.await();
            // todo 流式调用本来就不应该等待线程结束 这里为了测试就这样搞
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String result = sb.toString();
        System.err.println("这里的结果是"+result);

        return result;
    }


    @GetMapping("/dubboClientStream")
    public String dubboClientStream(){
        final String myName = this.name;
        StringBuffer sb=new StringBuffer();
        final CountDownLatch latch = new CountDownLatch(1);
        StreamObserver<String> request=streamService.dubboClientStream(new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.err.println(myName+"将RESULT{"+data+"}入库处理");
                sb.append(myName+"将RESULT{"+data+"}入库处理");
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.err.println(myName+"收到结束反馈");
                sb.append(myName+"收到结束反馈");
                latch.countDown();
            }
        });
        request.onNext(myName+"提出的问题1");
        request.onNext(myName+"提出的问题2");
        request.onNext(myName+"提出的问题3");
        request.onNext(myName+"提出的问题4");
        request.onCompleted();

        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return sb.toString();
    }

}
